package com.zut.hospital.controller.diagnose;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zut.hospital.dao.diagnose.Mdc_Rec_RelationMapper;
import com.zut.hospital.dao.diagnose.PatientMapper;
import com.zut.hospital.dao.diagnose.RecordMapper;
import com.zut.hospital.dao.pharmacy.MedicineMapper;
import com.zut.hospital.entity.diagnose.*;
import com.zut.hospital.entity.pharmacy.Medicine;
import com.zut.hospital.entity.pharmacy.MedicineExample;
import com.zut.hospital.service.diagnose.PatientService;
import com.zut.hospital.service.diagnose.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author 侯捷
 * @package_name com.zut.hospital.controller.diagnose
 * @date 2019/6/10 22:45
 * @description
 */
@Controller
@RequestMapping("/hospital/record")
public class RecordController {
    @Autowired
    RecordService recordService;
    @Autowired
    PatientService patientService;
@Autowired
    RecordMapper recordMapper;
@Autowired
    Mdc_Rec_RelationMapper mdc_rec_relationMapper;
@Autowired
    MedicineMapper medicineMapper;
@Autowired
    PatientMapper patientMapper;


    @ResponseBody
    @RequestMapping(value = "register/insert",method = RequestMethod.POST)
    public int insertOneRecord(@RequestBody String data){
        Record record=new Record();
        JSONObject jsonObject=JSON.parseObject(data);
        int patientId=jsonObject.getInteger("patientId");
        System.out.println(patientId);
//        String conven=jsonObject.getString("patConven");
        int phyId=jsonObject.getInteger("userId");
        System.out.println(phyId);
        String recResult=jsonObject.getString("recResult");
        String recRemark=jsonObject.getString("recRemark");
        int phyId1=jsonObject.getInteger("mdcCount");
        System.out.println(phyId);
        String recResult1=jsonObject.getString("mdcName");

        PatientExample patientExample=new PatientExample();
        patientExample.createCriteria().andIdEqualTo(patientId);
        Patient patient=patientMapper.selectByExample(patientExample).get(0);
        patient.setPatStatus(2);
        patientMapper.updateByExample(patient,patientExample);

        MedicineExample medicineExample=new MedicineExample();
        MedicineExample.Criteria criteria4=medicineExample.createCriteria();
        criteria4.andMdcNameEqualTo(recResult1);
        Medicine medicine=medicineMapper.selectByExample(medicineExample).get(0);
//        record.setRecPatId(patientService.getPatientIdByConven(conven));
//        record.setRecPhysicanId(Integer.parseInt(phyId));
        record.setRecPatId(patientId);
        record.setRecPhysicanId(phyId);
        record.setRecResult(recResult);
        record.setRecRemark(recRemark);
        recordService.insertOneRecord(record);
        RecordExample recordExample=new RecordExample();
        RecordExample.Criteria criteria=recordExample.createCriteria();
        criteria.andRecPatIdEqualTo(patientId);
        Record record1=recordMapper.selectByExample(recordExample).get(0);

        Mdc_Rec_Relation mdc_rec_relation=new Mdc_Rec_Relation();
        mdc_rec_relation.setRelatRecId(record1.getId());
        mdc_rec_relation.setRelatMdcCount(phyId1);
        mdc_rec_relation.setRelatMdcId(medicine.getId());




        return  mdc_rec_relationMapper.insert(mdc_rec_relation);
    }

    @ResponseBody
    @GetMapping(value = "register/user")
    public String getUserInfoByName(HttpSession session){
      String name=session.getAttribute("adminUser").toString();
//        recordService.getUserInfoByName(name);
        return recordService.getUserInfoByName(name);
    }

    @ResponseBody
    @GetMapping(value = "register/get/patient")
    public String getPatientByName(HttpSession session){
        String name=session.getAttribute("adminUser").toString();
        int id=recordService.getOfcIdBySession(name);
        return recordService.getInfoBySession(id);
    }

    @ResponseBody
    @GetMapping(value = "all/page")
    public String find(@RequestParam("page") int page, @RequestParam("limit") int limit,HttpSession session){
        String name=session.getAttribute("adminUser").toString();
        int id=recordService.getOfcIdBySession(name);
        return recordService.pageResult(id,page,limit);
    }
//    @ResponseBody
//    @GetMapping(value = "all/page1")
//    public String find(@RequestParam("page") int page, @RequestParam("limit") int limit,HttpSession session,@RequestParam("limit") String name1){
//        String name=session.getAttribute("adminUser").toString();
//        int id=recordService.getOfcIdBySession(name);
//        return recordService.pageResult(id,page,limit,name1);
//    }
    @ResponseBody
    @GetMapping(value = "get/conven")
    public String getConven(HttpSession session){
        String name=session.getAttribute("adminUser").toString();
        int id=recordService.getOfcIdBySession(name);
        return recordService.getConvenByOfcIdAndStatus(id,1);
    }

    @ResponseBody
    @RequestMapping(value = "get/info",method = RequestMethod.POST)
    public String getInfo(@RequestBody String data){
        JSONObject jsonObject=JSON.parseObject(data);
        int id=jsonObject.getInteger("id");
//        System.out.println(id+"**2.22222");
        return recordService.getPatientById(id);
    }

    @ResponseBody
    @GetMapping(value = "get/user")
    public String getUser(HttpSession session){
        String name=session.getAttribute("adminUser").toString();
        int id=recordService.getOfcIdBySession(name);
        return recordService.getAllUserByOfcId(id);
    }

    @ResponseBody
    @RequestMapping(value = "get/info1",method = RequestMethod.POST)
    public String getUser1(HttpSession session,@RequestBody String data){
        JSONObject jsonObject=JSON.parseObject(data);
        int status=jsonObject.getInteger("patStatus");
        String name=session.getAttribute("adminUser").toString();
        int id=recordService.getOfcIdBySession(name);
        return recordService.getInfoBySession1(id,status);
    }

    @ResponseBody
    @GetMapping(value = "get/record")
    public String getRecord(HttpSession session){
        String name=session.getAttribute("adminUser").toString();
        return recordService.getRecordByTimeAndId(name);
    }

    @ResponseBody
    @GetMapping(value = "get/record1")
    public String getRecord1(HttpSession session){
        String name=session.getAttribute("adminUser").toString();
        return recordService.getAllRecord(name);
    }

    @ResponseBody
    @RequestMapping(value = "delete/record",method = RequestMethod.POST)
    public int deletePatient(@RequestBody String data){
        JSONObject jsonObject=JSON.parseObject(data);
        int id=jsonObject.getInteger("id");

        return recordService.deleteOne(id);
    }
}
